package com.elinkway.infinitemovies.http.parser;

import android.text.TextUtils;

import com.elinkway.infinitemovies.bean.PushData;
import com.lvideo.http.exception.DataIsErrException;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;

public class PushDataParser extends LVideoMobileParser<PushData> {

	@Override
    public PushData parse(JSONObject data) throws Exception {
        String status = data.optString("status");
        if (!status.equals("200")) {
            throw new DataIsErrException("push data response status is not 200");
        }
        // data = data.getJSONObject("data");
        String json = data.getString("data");
        data = new JSONObject(json);
        PushData pushData = new PushData();
        pushData.setAt(data.optString("at"));
        pushData.setId(data.optString("id"));
        pushData.setTime(data.optString("time"));
        pushData.setTitle(data.getString("title"));
        pushData.setMsg(data.getString("msg"));
        pushData.setType(data.optString("type"));
        String resid = data.optString("resid");
        pushData.setResid(resid);
        pushData.setNeedJump(data.optString("needJump"));
        pushData.setLiveEndData(data.optString("liveEndDate"));
        pushData.setCid(data.optString("cid"));
        pushData.setPicUrl(data.optString("picUrl"));
        pushData.setFrequency(data.getInt("frequency"));


        JSONArray streamsArray = data.optJSONArray("streams");
        if ( (null != streamsArray && streamsArray.length() > 0) || !TextUtils.isEmpty(resid)) {
            ArrayList<String> streamsList = new ArrayList<String>();
            if (!TextUtils.isEmpty(resid)) {
                streamsList.add(resid);
            }
            if(null != streamsArray && streamsArray.length() > 0){
            	int streamsSize = streamsArray.length();
                for (int i = 0; i < streamsSize; i++) {
                    String url = streamsArray.optString(i);
                    if (!TextUtils.isEmpty(url)) {
                        streamsList.add(url);
                    }
                }
            }
            
            pushData.setLiveStreams(streamsList);
        }
        return pushData;
    }

}
